package com.example.management.common.controller

import com.example.management.common.config.Constant
import com.example.management.common.domain.DictDO
import com.example.management.common.service.DictService
import com.example.management.common.utils.PageUtils
import com.example.management.common.utils.Query
import com.example.management.common.utils.R
import org.apache.shiro.authz.annotation.RequiresPermissions
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import springfox.documentation.annotations.ApiIgnore
import java.util.HashMap

@ApiIgnore
@Controller
@RequestMapping("/common/dict")
class DictController : BaseController() {
    @Autowired
    private val dictService: DictService? = null

    @GetMapping
    @RequiresPermissions("common:dict:dict")
    internal fun dict(): String {
        return "common/dict/dict"
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("common:dict:dict")
    fun list(@RequestParam params: Map<String, Any>): PageUtils {
        // 查询列表数据
        val query = Query(params)
        val dictList = dictService!!.list(query)
        val total = dictService!!.count(query)
        return PageUtils(dictList, total)
    }

    @GetMapping("/add")
    @RequiresPermissions("common:dict:add")
    internal fun add(): String {
        return "common/dict/add"
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("common:dict:edit")
    internal fun edit(@PathVariable("id") id: Long?, model: Model): String {
        val dict = dictService!!.get(id)
        model.addAttribute("dict", dict)
        return "common/dict/edit"
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("common:dict:add")
    fun save(dict: DictDO): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        return if (dictService!!.save(dict) > 0) {
            R.ok()
        } else R.error()
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("common:dict:edit")
    fun update(dict: DictDO): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        dictService!!.update(dict)
        return R.ok()
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("common:dict:remove")
    fun remove(id: Long?): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        return if (dictService!!.remove(id) > 0) {
            R.ok()
        } else R.error()
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("common:dict:batchRemove")
    fun remove(@RequestParam("ids[]") ids: Array<Long>): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        dictService!!.batchRemove(ids)
        return R.ok()
    }

    @GetMapping("/type")
    @ResponseBody
    fun listType(): List<DictDO> {
        return dictService!!.listType()
    }

    // 类别已经指定增加
    @GetMapping("/add/{type}/{description}")
    @RequiresPermissions("common:dict:add")
    internal fun addD(model: Model, @PathVariable("type") type: String, @PathVariable("description") description: String): String {
        model.addAttribute("type", type)
        model.addAttribute("description", description)
        return "common/dict/add"
    }

    @ResponseBody
    @GetMapping("/list/{type}")
    fun listByType(@PathVariable("type") type: String): List<DictDO> {
        // 查询列表数据
        val map = HashMap<String, Any>(16)
        map["type"] = type
        return dictService!!.list(map)
    }
}